package com.example.service.tribune;

import com.example.common.enums.PostStatusEnum;
import com.example.common.enums.RoleEnum;
import com.example.mapper.tribune.PostMapper;

import com.example.model.entity.other.Account;
import com.example.model.entity.tribune.Post;
import com.example.model.entity.tribune.PostSuccess;
import com.example.model.vo.PostVO;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 论坛帖子信息表业务处理
 **/
@Service
public class PostService {

    @Resource
    private PostMapper postMapper;

    /**
     * 新增
     */
    public void add( Post post) throws ParseException {
        Account currentUser = TokenUtils.getCurrentUser();
        if(RoleEnum.DOCTOR.name().equals(currentUser.getRole())){
            post.setRoleId(currentUser.getId());
            post.setStatus(PostStatusEnum.STATUS_ING.getName());
            String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
            post.setPostTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(format));
            post.setLookNum(0);
            post.setClickNum(0);
            post.setCollectNum(0);
        }
        postMapper.insert(post);
    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        postMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            postMapper.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(Post post) {
        postMapper.updateById(post);
    }

    /**
     * 根据ID查询
     */
    public PostVO selectById(Integer id) {
        return postMapper.selectById(id);
    }
    public PostSuccess selectSuccessById(Integer roleId) {
        return postMapper.selectSuccessById(roleId);
    }
    /**
     * 查询所有
     */
    public List<Post> selectAll(Post post) {
        Account currentUser = TokenUtils.getCurrentUser();
        if(RoleEnum.DOCTOR.name().equals(currentUser.getRole())){
            post.setRoleId(currentUser.getId());
        }
        return postMapper.selectAll(post);
    }
    public List<Post> selectFrontPost(Integer typeId) {
        return postMapper.selectFrontPost(typeId);
    }
    public List<Post> selectRandomPost(Post post) {
        return postMapper.selectRandomPost(post);
    }

    public List<Post> selectTopPost(Post post) {
        return postMapper.selectTopPost(post);
    }
    /**
     * 分页查询
     */
    public PageInfo<Post> selectPage(Post post, Integer pageNum, Integer pageSize) {
        Account currentUser = TokenUtils.getCurrentUser();
        if(RoleEnum.DOCTOR.name().equals(currentUser.getRole())){
            post.setRoleId(currentUser.getId());
        }
//        post.setStatus(PostStatusEnum.STATUS_SUCCESS.getName());
        PageHelper.startPage(pageNum, pageSize);
        List<Post> list = postMapper.selectAll(post);
        return PageInfo.of(list);
    }


    public List<Post> selectByTypeId(Integer typeId) {
        return postMapper.selectByTypeId(typeId);
    }


    public void updateCount(Integer id) {
        postMapper.updateCount(id);
    }
}
